<?php
class Model_League extends Zend_Db_Table_Abstract
{
	protected $_name    = 'g1_league';
	protected $_primary = 'id';


	public function getAllActiveLeagues($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()
						->where('status=?', 'new')
						->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getAllLeagues($order = 'desc'){
		if($order=='desc'){
			$order='desc';
		}else{
			$order='asc';
		}
		$select = $this->select()		
		->order('id '.$order);
		$row = $this->fetchAll($select);
		return $row;
	}
	
	public function getLeagueById($league_id){
		$select = $this->select()
		               ->where('id=?',$league_id);
		$row = $this->fetchRow($select);
		return $row;
	}
	
	public function createNewLeague($league_name,$team_count){
		
		$data['name'] = $league_name;
		$data['team_count'] = $team_count;
		$data['status'] = 'new';
			
		$this->insert($data);
		$league_id = $this->getAdapter()->lastInsertId();
		
		$model_league_team = new Model_LeagueTeam();
		$count = 1;
		for($count=1;$count<$team_count +1;$count++){
			$model_league_team->addTeam($league_id,
					$count,
					'',
					null);
		}
		
		return $league_id;
		
	}
	
	public function closeLeagueById($league_id){
		$data = array('status'  => 'closed');
		
		$this->update($data, 'id='.$league_id);
	}
	
	public function getFirstLeagueId(){
		$select = $this->select('id');
	
		$row = $this->fetchRow($select);
		return $row;
	}
}